from pydantic import BaseSettings, Field
from typing import Any, Dict, List, Optional
import secrets
from pathlib import Path

class Settings(BaseSettings):
    # 项目基本信息
    PROJECT_NAME: str = "幻兽帕鲁开服器"
    API_V1_STR: str = "/api/v1"
    VERSION: str = "1.0.0"
    DESCRIPTION: str = "幻兽帕鲁开服器管理平台API接口"

    # 服务器配置
    SERVER_HOST: str = "0.0.0.0"
    SERVER_PORT: int = 8000
    RELOAD: bool = True

    # 数据库配置
    DATABASE_URL: str = Field(..., env="DATABASE_URL")
    DB_ECHO: bool = False

    # Redis配置
    REDIS_URL: str = Field(..., env="REDIS_URL")
    REDIS_CACHE_TTL: int = 300  # 默认缓存时间5分钟

    # 安全配置
    SECRET_KEY: str = Field(default_factory=secrets.token_urlsafe(32))
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 15
    REFRESH_TOKEN_EXPIRE_DAYS: int = 7
    ALGORITHM: str = "HS256"

    # CORS配置
    CORS_ORIGINS: List[str] = Field(default_factory=lambda: ["*"])

    # 日志配置
    LOG_LEVEL: str = "INFO"
    LOG_FILE: Optional[str] = None

    # 游戏服务器配置
    STEAMCMD_PATH: Path = Path("scripts/steamcmd")
    SERVER_INSTALL_PATH: Path = Path("servers/palworld")
    DEFAULT_SERVER_PORT: int = 8211
    DEFAULT_RCON_PORT: int = 25575

    class Config:
        case_sensitive = True
        env_file = ".env"
        env_file_encoding = "utf-8"

settings = Settings()

# 确保必要的目录存在
settings.STEAMCMD_PATH.mkdir(parents=True, exist_ok=True)
settings.SERVER_INSTALL_PATH.mkdir(parents=True, exist_ok=True)